package com.desaysv.wandroid.http

import android.annotation.SuppressLint
import android.content.Context
import com.alibaba.fastjson.JSONObject
import com.desaysv.svhttp.SVHttp
import com.desaysv.svhttp.coder.IResponseDecoder
import com.desaysv.wandroid.http.interceptor.LogInterceptor
import java.lang.reflect.Type

/**
 * @author linzi
 * @date 2022-11-9
 */
@SuppressLint("StaticFieldLeak")
object HttpManager {
    private var mContext: Context?=null
    private lateinit var mService: WAndroidService
    @SuppressLint("StaticFieldLeak")
    fun init(context: Context) {
        mContext=context
        mService=SVHttp
            .Builder()
            .setBaseUrl(HttpContent.BASE_URL)
            .setInterceptor(LogInterceptor())
            .setResponseCoder(object :IResponseDecoder{
                override fun <T : Any?> decoder(response: ByteArray?, t: Class<T>?): T {
                    return JSONObject.parseObject(response,t)
                }

                override fun <T : Any?> decoder(response: ByteArray?, t: Type?): T {
                    return JSONObject.parseObject(response,t)
                }
            })
            .build()
            .create(WAndroidService::class.java)

    }
    fun getService():WAndroidService{
        return mService
    }
}